package net.sky.report.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sky.report.bean.BasSchool;
import net.sky.report.bean.BasSubject;
import net.sky.report.bean.BasTerm;
import net.sky.report.bean.SumTimes;
import net.sky.report.bean.TimeTable;
import net.sky.report.dao.TimeTableDao;

public class TimeReportServlet extends HttpServlet {

	private static final long serialVersionUID = -5728800063771790592L;

	/**
	 * Constructor of the object.
	 */
	public TimeReportServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		HttpSession session=request.getSession();
		
		String teaName=request.getParameter("teaName");
		String schID=request.getParameter("schID");
		String terID=request.getParameter("terID");
		String subID=request.getParameter("subID");

		int schId=0;
		if(schID!=null){
			schId=Integer.parseInt(schID);
			session.setAttribute("schId",schId);
		}else{
			Integer scId=(Integer)session.getAttribute("schId");
			if(scId!=null)
				schId=scId;			
		}
		int terId=0;
		if(terID!=null){
			terId=Integer.parseInt(terID);
			session.setAttribute("terId",terId);
		}else{
			Integer tId=(Integer)session.getAttribute("terId");
			if(tId!=null)
				terId=tId;			
		}
		int subId=0;
		if(subID!=null){
			subId=Integer.parseInt(subID);
			session.setAttribute("subId",subId);
		}else{
			Integer suId=(Integer)session.getAttribute("subId");
			if(suId!=null)
				subId=suId;			
		}
		TimeTableDao ttd=new TimeTableDao();
		
		List<TimeTable> li=null;
		SumTimes st=null;
		if(teaName!=null){
			li=ttd.searchAllByteaName(teaName);
			st=ttd.getSumByteaName(teaName);
		}else{
		    li=ttd.searchAll(schId,terId,subId);
		    st=ttd.getSum(schId, terId, subId);
		}
		List<BasSchool> schli=ttd.searchSchool();
		List<BasTerm> terli=ttd.searchTerm();
		List<BasSubject> subli=ttd.searchSubject();
		
		request.setAttribute("li", li);
		request.setAttribute("schli", schli);
		request.setAttribute("terli", terli);
		request.setAttribute("subli", subli);
		request.setAttribute("st", st);
		request.setAttribute("teaName", teaName);
		request.getRequestDispatcher("../dataReport/index.jsp").forward(request, response);
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
}
